import 'dart:convert';
import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/shop/life/GoodsDetail.dart';

class ZitiDetailPage extends StatefulWidget {
  ZitiDetailPage({Key key, this.ztid}) : super(key: key);

  final String ztid;

  @override
  _ZitiDetailPage createState() => _ZitiDetailPage(ztid);
}

class _ZitiDetailPage extends State<ZitiDetailPage> {
 // List goods = [{"gid":100,"gname":"苹果","gprice":100},{"gid":101,"gname":"苹果1","gprice":200},{"gid":102,"gname":"苹果2","gprice":300},{"gid":103,"gname":"苹果3","gprice":400},];
  var reid;   // 退款退货编号
  var item;   // 退货详情
  // 构造函数
  _ZitiDetailPage(id){
    this.reid=id;
    _onLoading();
  }
  // 加载数据的函数
  void _onLoading() async{
    var x = await HttpUtil().get("/app/refunds/selectRefundById?id="+reid);
    if(x['code']==200){
      item=x['data'];
      setState(() {

      });
    }

  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        title: Text("自提点详情"),
      ),
      body: Container(
        padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
        child: Column(
          children: [
            Container(
              // height: ScreenUtil().setHeight(100),
              width: double.infinity,
              margin: EdgeInsets.all(ScreenUtil().setWidth(10)),
              padding: EdgeInsets.symmetric(vertical: ScreenUtil().setHeight(30),horizontal: ScreenUtil().setHeight(10)),
              decoration: BoxDecoration(
                  color: Colors.white
              ),
              child: Column(
                children: [
                  Row(children: [
                    Icon(Icons.check_circle,color: Colors.orange,),
                    // Text("退款成功")
                    _showRefundStatus(item['refund_status'])
                  ],),
                  Row(children: [
                    // Text(item['refund_sqTime'])
                    _showRefundTime()
                  ],)
                ],
              ),
            ),
            SizedBox(height: ScreenUtil().setWidth(10),),
            Container(
              margin: EdgeInsets.all(ScreenUtil().setHeight(10)),
              padding: EdgeInsets.all(ScreenUtil().setHeight(10)),
              width: double.infinity,
              decoration: BoxDecoration(
                color: Colors.white
              ),
              child: Column(
                children: [
                  Row(children: [Text("退款信息",style: TextStyle(fontWeight: FontWeight.bold),)],),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      // CachedNetworkImage(imageUrl: "https://www.itying.com/images/flutter/list2.jpg",width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                      CachedNetworkImage(imageUrl: Config.getInstance().getHost()+item['goods']['gimages'],width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Column(
                        children: [
                          Text(item['goods']['gname']),
                          Text(item['orderDetail']['guiges']==null?"":item['orderDetail']['guiges'],style: TextStyle(color: Colors.black38),),
                        ],
                      )
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("退款原因"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      // Text("不喜欢，不爱了"),
                      _showRefundReason(item['refund_reason'])
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("退款金额"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Text("￥${item['order']['tuikuanMoney']}"),
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("申请件数"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Text(item['refund_number'].toString()),
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("照片凭证"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          item['picture1']==null?Text(""):  CachedNetworkImage(imageUrl: "https://www.itying.com/images/flutter/list2.jpg",width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                          item['picture2']==null?Text(""):  CachedNetworkImage(imageUrl: "https://www.itying.com/images/flutter/list2.jpg",width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                          item['picture3']==null?Text(""):  CachedNetworkImage(imageUrl: "https://www.itying.com/images/flutter/list2.jpg",width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                        ],
                      )
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("申请时间"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Text(item['refund_sqTime']),
                    ],
                  ),

                ],
              ),
            )

          ],
        ),
      )
    );
  }
  // 动态显示售后类型
  Text _showRefundType(type) {
    var s = "退款";   // 默认退款
    if(type==1)
      s="退货";
    else if(type==2)
      s="换货";
    else if(type==3)
      s="退款不退货";
    else if(type==4)
      s="取消订单";
    return Text(s);
  }
  // 动态显示售后状态
  Text _showRefundStatus(status) {
    var s = "申请中";   // 默认
    if(status==1)
      s="申请未通过";
    else if(status==2)
      s="等待商家收货";
    else if(status==3)
      s="商家已收货";
    else if(status==4)
      s="商家发货";
    else if(status==5)
      s="客户收货";
    else if(status==6)
      s="退款成功";
    else if(status==7)
      s="退货成功";
    return Text(s);
  }
  // 动态显示退款原因
  Text _showRefundReason(reason) {
    var s = "其他";   // 默认退款
    if(reason==1)
      s="七天无理由退货";
    else if(reason==2)
      s="产品质量问题";
    else if(reason==3)
      s="发票问题";
    else if(reason==4)
      s="不喜欢了/不爱了";
    else if(reason==5)
      s="拍错了/看错了";
    else if(reason==6)
      s="商家没发货";
    return Text(s);
  }
  // 动态显示售后状态
  Text _showRefundTime() {
    var s = "申请中";   // 默认
    var status= item['refund_status'];
    if(status==1)
      s=item['refund_disagree_reason'];
    else if(status==2)
    {
      var t;
      t= item['user_fahuo_time']==null? "":item['user_fahuo_time'];
      s="寄货时间"+t;
    }
    else if(status==3)
      s="商家已收货";
    else if(status==4)
     {
       var t;
       t= item['admin_fahuo_time']==null? "":item['admin_fahuo_time'];
       s="换货已发"+t;
     }
    else if(status==5)
      s="已收货，请到订单页面确认收货";
    else if(status==6)
      s=item['order']['orFinishtTime']==null? "":item['order']['orFinishtTime'];
    else if(status==7)
      s=item['order']['orFinishtTime']==null? "":item['order']['orFinishtTime'];
    return Text(s);
  }
}